<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>发布新投票</title>
	<link type="text/css" rel="stylesheet" href="css/style.css" />
	<script src="http://47.106.66.89:8080/js/vue.min.js"></script>
	<script src="http://47.106.66.89:8080/js/axios.min.js"></script>
	<link  href="http://47.106.66.89:8080/js/ele-2.15.7/theme-chalk/index.css" rel="stylesheet">
	<script src="http://47.106.66.89:8080/js/ele-2.15.7/index.js"></script>
	<script src="js/vote.js"></script>
</head>
<body>
<div id="app">
	<vote>
		<div id="voteManage" class="box">
			<h2>添加新投票</h2>
			<div class="content">
				<form method="post" action="view.html">
					<dl>
						<dt>投票内容：</dt>
						<dd>
							<input type="hidden" name="entityId" value=""/>
							<input type="text" class="input-text" name="subject.title" v-model="vote.vname" />
							<br/>
							<span v-if="showErrors && !vote.vname.trim()" class="error-message">投票内容不能为空</span>
						</dd>
						<dt>投票类型：</dt>
						<dd>
							<input type="radio" name="vote.vtype" value="单选" v-model="vote.vtype" />单选
							<input type="radio" name="vote.vtype" value="多选" v-model="vote.vtype" />多选
							<br/>
							<span v-if="showErrors && !vote.vtype" class="error-message">请选择投票类型</span>
						</dd>
						<dt>投票选项：</dt>

						<dd id="voteoptions">
							<p v-for="(i, index) in vote.items" :key="index">
								<input type="text" class="input-text" v-model="i.iname" />
								<br/>
								<span v-if="!i.iname.trim()" class="error-message">选项不能为空</span>
							</p>
						</dd>


						<dt></dt>
						<dd class="button">
							<input type="image" src="images/button_submit.gif" @click.prevent="validateAndSubmit" />
							<a href="#" @click.prevent="vote.items.push({iname:''})">增加选项</a>
							<a href="index.html">取消操作</a>
						</dd>
					</dl>
				</form>
			</div>
		</div>
	</vote>
</div>
<script>
	var app = new Vue({
		el: '#app',
		data: {
			vote:{
				vname: "",vtype: "",startDate: new Date(Date.now() + 24*60*60*1000).toISOString().split('T')[0],endDate: new Date(Date.now() + 7*24*60*60*1000).toISOString().split('T')[0],items: [{iname:""},{iname:""}]
			},
			showErrors: false
		},
		methods: {
			validateAndSubmit() {
				// 重置错误显示状态
				this.showErrors = true;
				
				// 检查投票内容是否为空
				if (!this.vote.vname || this.vote.vname.trim() === '') {
					this.$alert('请填写投票内容');
					return;
				}
				
				// 检查投票类型是否选择
				if (!this.vote.vtype) {
					this.$alert('请选择投票类型');
					return;
				}
				
				// 检查是否有任何选项为空
				const hasEmptyOption = this.vote.items.some(item => !item.iname || item.iname.trim() === '');
										
				if (hasEmptyOption) {
					// 如果有选项为空，不提交表单，错误信息会通过v-if自动显示
					this.$alert('请填写所有选项');
				} else {
					// 所有选项都不为空，提交表单
					this.add();
				}
			},
			add() {
				axios.post("/vote/add", this.vote).then(res => {
					if(res.data.code == 1) {
						location.href = "index.html"
					} else {
						this.$alert(res.data.msg);
					}
				})
			}
		}
	});
</script>
</body>
</html>

